package com.sxfech.project.auth.config;

import com.sxfech.project.auth.service.OAuthUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationTrustResolver;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private OAuthUserDetailService userDetailService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http.requestMatchers()
//                .and().authorizeRequests().anyRequest().authenticated()
//                .antMatchers("/oauth/**", "/favicon.ico",
//                "/error", "/users/create")
//                .permitAll().and().logout().permitAll()
//                .and().formLogin().loginPage("/login").permitAll()
//                .and().csrf().disable().httpBasic();

        http.antMatcher("/**")
                .authorizeRequests().antMatchers( "/oauth/token**","/test/**").permitAll()
                .anyRequest().authenticated()
                .and().exceptionHandling()
                .accessDeniedPage("/login?authorization_error=true")
                .and()
                .csrf().disable()
                .logout()
                .permitAll()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .authorizeRequests()
                .anyRequest().authenticated().and();

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        List<String> ignoringArr = Arrays.asList(new String[]{
                "/**/*.js","/**/*.ico","/**/*.woff","/**/*.json","/**/*.map",
                "/**/*.css","/**/*.jpg","/**/*.png","/**/*.gif", "/**/favicon.ico",
                "/resources/**, /webjars/**","/error","/developer/**","/oauth/check_token"
        });
        List newIgnoring = new ArrayList();
        newIgnoring.addAll(ignoringArr);
        String []igs = (String [])newIgnoring.toArray(new String[]{});
        web.ignoring().antMatchers( igs);
    }
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }



    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
